package ldh.im.fxbase.util;

import java.lang.ref.SoftReference;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;

public class SoftMapUtil {

    private static final ConcurrentHashMap<String, SoftReference<RegionUtil.FxmlBean>> softMap = new ConcurrentHashMap<>();
    private static final WeakHashMap<String, RegionUtil.FxmlBean> weakMap = new WeakHashMap<>();

    public static RegionUtil.FxmlBean getCache(String key) {
        SoftReference<RegionUtil.FxmlBean> softReference = softMap.get(key);
        if (softReference == null) return null;
        RegionUtil.FxmlBean fxmlBean = softReference.get();
        return fxmlBean;
    }

    public static void cache(String key, RegionUtil.FxmlBean fxmlBean) {
        SoftReference<RegionUtil.FxmlBean>softReference = new SoftReference<>(fxmlBean);
        softMap.put(key, softReference);
    }

    public static RegionUtil.FxmlBean getWeakCache(String key) {
        RegionUtil.FxmlBean fxmlBean = weakMap.get(key);
        return fxmlBean;
    }

    public static void cacheWeak(String key, RegionUtil.FxmlBean fxmlBean) {
        weakMap.put(key, fxmlBean);
    }
}
